<%@ include file="/common/taglibs.jsp"%>
<script type="text/javascript">var batchexportMessage ="<fmt:message key="cc.batchexport.message"/>";</script>
<script type="text/javascript">var pbocMessage ="<fmt:message key="cc.transferCA.message"/>";</script>

<head>
    <title><fmt:message key="loanRequestList.title"/></title>
    <meta name="menu" content="LoanRequestListMenu"/>
    <style>
     tr.highlight {
    font-weight: bold;
    background-color: red !important
    }
        tr.mediumhigh {
            background-color: #FFB6C1 !important
        }
    </style>
</head>

<c:if test="{'$'}{not empty searchError}">
    <div class="alert alert-danger alert-dismissable">
        <a href="#" data-dismiss="alert" class="close">&times;</a>
        <c:out value="{'$'}{searchError}"/>
    </div>
</c:if>
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        <fmt:message key="loanRequestList.heading"/>
        <small><p><fmt:message key="loanRequestList.message"/></p></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i><fmt:message key="loanRequestList.heading"/></a></li>
        <li class="active"> <fmt:message key="loanRequestList.heading"/></li>
    </ol>
</section>
<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <h3 class="box-title"> <fmt:message key="loanRequestList.heading"/></h3>
                </div><!-- /.box-header -->
                <div class="box-body">
<security:authorize ifAnyGranted="ROLE_DEALER_USER,ROLE_DEALER_ADMIN">
    <c:if test="${param.phase == 'dealer_apply_loan'}">
                    <p><fmt:message key="dealer_apply_loan.message"/></p>
    </c:if>
    </security:authorize>
                    <security:authorize ifAnyGranted="ROLE_CC">
                    <form action="batchExport" method="get">
                        </security:authorize>
                        <security:authorize ifAnyGranted="ROLE_Bank_USER">
                        <c:if test="${param.phase == 'bank_credit_audit'}">
                        <form action="batchAudit" method="get">
                            </c:if>
                            </security:authorize>

                    <display:table name="loanRequestList" class="table table-bordered table-hover" requestURI="" id="loanRequestList" export="false" pagesize="25">
                        <security:authorize ifAnyGranted="ROLE_CC">
                        <display:column titleKey="button.select" sortable="false" style="width: 3%;">
                            <input type="checkbox" name="selectedIds" value="<c:out value="${loanRequestList.id}"/>"/>
                        </display:column>
                        </security:authorize>
                        <security:authorize ifAnyGranted="ROLE_Bank_USER">
                            <c:if test="${param.phase == 'bank_credit_audit' or param.phase == 'bank_pending_loan'}">
                            <display:column titleKey="button.select" sortable="false" style="width: 3%;">
                                <input type="checkbox" name="selectedIds" value="<c:out value="${loanRequestList.id}"/>"/>
                            </display:column>
                            </c:if>
                        </security:authorize>
                        <display:column property="number" sortable="true" href="loanRequestform" media="html" style="width: 8%;"
                                        paramId="reqId" paramProperty="id" titleKey="loanRequest.number"/>
                        <display:column property="id" media="csv excel xml pdf" titleKey="loanRequest.id"/>
                        <display:column property="requestPerson.name" sortable="true" titleKey="loanRequest.requestUser" style="width: 7%;"/>
                        <display:column property="dealer.name" sortable="true" titleKey="loanRequest.dealer" style="width: 8%;"/>
                        <display:column property="dealerUser.username" sortable="true" titleKey="loanRequest.dealerUser" style="width: 8%;"/>
                        <display:column sortable="true" titleKey="loanRequest.idType" style="width: 8%;"><fmt:message key="ID_TYPE_${loanRequestList.requestPerson.idType}"/></display:column>
                        <display:column property="requestPerson.idNo" sortable="true" titleKey="loanRequest.idNo" style="width: 9%;"/>
                        <display:column sortable="true" titleKey="loanRequest.status" style="width: 8%;"><fmt:message key="REQUEST_STATUS_${loanRequestList.status}"/></display:column>
                        <%--<display:column property="auditUser.username" sortable="true" titleKey="loanRequest.auditUser" style="width: 7%;"/>--%>
                        <display:column sortProperty="requestDate" sortable="true" titleKey="loanRequest.requestDate" style="width: 7%;">
                            <fmt:formatDate value="${loanRequestList.requestDate}" pattern="${datePattern}"/>
                        </display:column>
                        <c:if test="${param.phase == 'insurance_upload_policy'}">
                            <display:column sortProperty="loanDate" sortable="true" titleKey="loanRequest.loanDate" style="width: 7%;">
                                <fmt:formatDate value="${loanRequestList.loanDate}" pattern="${datePattern}"/>
                            </display:column>
                        </c:if>
                        <%--<display:column sortable="true" titleKey="loanRequest.feedbackType" style="width: 9%;"><fmt:message key="FEEDBACK_STATUS_${loanRequestList.feedbackType}"/></display:column>--%>
                        <display:column titleKey="loanRequest.changeApprove" style="width: 9%; padding-left: 15px" media="html">
                            <input type="checkbox" disabled="disabled" <c:if test="${loanRequestList.auditCode == 'changeApprove'}">checked="checked"</c:if>/>
                        </display:column>
                        <security:authorize ifNotGranted="ROLE_DEALER_USER">
                        <display:column titleKey="loanRequest.sendBack" style="width: 5%;" media="html">
                            <input type="checkbox" disabled="disabled" <c:if test="${loanRequestList.sendback}">checked="checked"</c:if>/>
                        </display:column>
                        </security:authorize>
                        <display:column sortable="false" titleKey="loanRequestDetail.actionTitle">
                        <security:authorize ifAnyGranted="ROLE_DEALER_USER">
                            <c:if test="${loanRequestList.phase != 'draft' and loanRequestList.phase != 'dealer_submit' and loanRequestList.phase != 'bank_credit_audit'}">
                                    <a class="btn btn-primary btn-xs" href='/copyRequest?reqId=${loanRequestList.id}'><fmt:message key="loanRequestList.copy"></fmt:message></a>
                            </c:if>
                        </security:authorize>
                        </display:column>
                        <security:authorize ifAnyGranted="ROLE_CC">
                            <display:column sortable="false" titleKey="loanRequestDetail.actionTitle">
                                <a class="btn btn-primary btn-xs" href="/attachments?reqId=${loanRequestList.id}"><fmt:message key="loanRequestDetail.action.seeFile"/></a>
                                <a class="btn btn-primary btn-xs" reqId=${loanRequestList.id} id="transferToCA"><fmt:message key="loanRequestDetail.action.transfer.ca"/></a>
                            </display:column>
                        </security:authorize>

                        <display:setProperty name="paging.banner.item_name"><fmt:message key="loanRequestList.loanRequest"/></display:setProperty>
                        <display:setProperty name="paging.banner.items_name"><fmt:message key="loanRequestList.loanRequests"/></display:setProperty>

                        <display:setProperty name="export.excel.filename"><fmt:message key="loanRequestList.title"/>.xls</display:setProperty>
                        <display:setProperty name="export.csv.filename"><fmt:message key="loanRequestList.title"/>.csv</display:setProperty>
                        <display:setProperty name="export.pdf.filename"><fmt:message key="loanRequestList.title"/>.pdf</display:setProperty>
                    </display:table>
                        <security:authorize ifAnyGranted="ROLE_CC">
                        <button type="submit" class="btn btn-primary" id="batchExport" name="batchExport" onclick="return checkBatch();">
                            <i class="icon-ok icon-white"></i> <fmt:message key="loanRequestDetail.action.exportPersonList"/>
                        </button>
                        </security:authorize>
                        <security:authorize ifAnyGranted="ROLE_Bank_USER">
                            <c:if test="${param.phase == 'bank_credit_audit'}">
                                <button type="submit" class="btn btn-primary" id="batchAudit" name="batchAudit" onclick="return checkBatch();">
                                    <i class="icon-ok icon-white"></i> <fmt:message key="loanRequestDetail.action.batchAudit"/>
                                </button>
                            </c:if>
                            <c:if test="${param.phase == 'bank_pending_loan'}">
                                <a class="btn btn-primary" id="batchLoaned" name="batchLoaned" >
                                    <i class="icon-ok icon-white"></i> <fmt:message key="loanRequestDetail.action.batchLoaned"/>
                                </a>
                            </c:if>
                        </security:authorize>
                                <security:authorize ifAnyGranted="ROLE_CC">
                        </form>
                                    </security:authorize>
                                    <security:authorize ifAnyGranted="ROLE_Bank_USER">
                                    <c:if test="${param.phase == 'bank_credit_audit' }">
                    </form>
                                        </c:if>
                                        </security:authorize>

                </div><!-- /.box-body -->
            </div><!-- /.box -->
        </div>
    </div>
</section><!-- /.content -->

<div id="transferCADlg" title="<fmt:message key="loanRequestDetail.action.transfer.ca"/>">
    <form class="form" action="transferCA" method="post">
        <input hidden id="reqId" name="reqId"/>
        <div class="form-group">
            <fmt:message key="loanRequest.pbocComment"/>
            <textarea rows="4" id="pbocComment" class="form-control" name="pbocComment"></textarea>
        </div>
        <button type="submit" class="btn btn-primary" id="transferCA" onclick="return checkPboc();"><fmt:message key="button.submit"/></button>
    </form>

</div>

<div id="bankLoanedDialog" title="<fmt:message key="loanRequestDetail.action.batchLoaned"/>">
    <p><fmt:message key="loanRequest.dateConfirm"/></p>
        <form class="form" action="batchLoaned" method="get">
        <input hidden id="batchLoanedIds" name="requestIds"/>
        <div class="form-group">
            <fmt:message key="loanRequest.loanDate"/>
            <input class="form-control datepicker" type="text" name="date"
                        id="date"
                        maxlength="255" />
        </div>
        <button type="submit" class="btn btn-primary" id="submitBankLoaned" ><fmt:message key="button.submit"/></button>
    </form>

</div>

<c:set var="scripts" scope="request">
<script type="text/javascript">
    $(function () {
        $('.datepicker').datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "mm/dd/yy",
            minDate: "-1d"
        });
        $("#date").val(moment(new Date()).format('MM/DD/YYYY'));
        $('#loanRequestList').dataTable({
            <c:if test="${param.phase == 'dealer_apply_loan'}">
            "createdRow": function ( row, data, index ) {
                if ( ((new Date()) - (new Date(data[7])))>1296000000 ) {
                    $(row).addClass('highlight');
                } else if(((new Date()) - (new Date(data[7])))>604800000 ){
                    $(row).addClass('mediumhigh');
                }
            },
            </c:if>
            <c:if test="${param.phase == 'insurance_upload_policy'}">
            "createdRow": function ( row, data, index ) {
                if ( ((new Date(data[8])) - (new Date()))<172800000 ) {
                    $(row).addClass('highlight');
                }
            },
            </c:if>
            "language": {
                "url": "//cdn.datatables.net/plug-ins/1.10.7/i18n/Chinese.json"
            },
            "bPaginate": true,
            "bLengthChange": false,
            "bFilter": false,
            "bSort": true,
            "bInfo": false,
            "bAutoWidth": false
        });

        dialogTransfer = $( "#transferCADlg" ).dialog({
            autoOpen: false,
            height: 300,
            width: 550,
            modal: true
        });

        $( "#transferToCA" ).click(function() {
            $("#reqId").val($(this).attr("reqId"));
            $("#pbocComment").val('');
            dialogTransfer.dialog( "open" );
        });
        var dialogBankLoan;
        dialogBankLoan = $('#bankLoanedDialog').dialog({
            autoOpen: false,
            height: 400,
            width: 550,
            modal: true
        });
        $("#batchLoaned").click(function(){
            //if(!checkBatch()) return false;
            var selectedIds = $("input[name='selectedIds']:checked").val();
            $("#batchLoanedIds").val(selectedIds);
            dialogBankLoan.dialog("open");
        })

    });

    function checkPboc() {
        var pbocCommentStr = $("#pbocComment").val();
        if (pbocCommentStr == null || pbocCommentStr == "") {
            alert(pbocMessage);
            return false;
        } else {
            return true;
        }
    }

    function checkBatch() {
        var selectedIds = $("input[name='selectedIds']:checked").val();
        if (!selectedIds || selectedIds.length == 0) {
            alert(batchexportMessage);
            return false;
        } else {
            return true;
        }
    }

</script>
</c:set>
